﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using Contract.Base;
using Contract.DTO;
using Contract.Request;
using Contract.Response;

namespace WebApplication1.Controllers
{
    /// <summary>
    /// 获取用户信息API
    /// </summary>
    public class DefaultController : ApiController
    {
        List<UserDTO> userDtos = new List<UserDTO>();
        public DefaultController()
        {
            userDtos.Add(new UserDTO() { UserId = 10001, UserName = "张三1" });
            userDtos.Add(new UserDTO() { UserId = 10002, UserName = "张三2" });
            userDtos.Add(new UserDTO() { UserId = 10003, UserName = "张三3" });
            userDtos.Add(new UserDTO() { UserId = 10004, UserName = "张三4" });
            userDtos.Add(new UserDTO() { UserId = 10005, UserName = "张三5" });
            userDtos.Add(new UserDTO() { UserId = 10006, UserName = "张三6" });
        }

        private TResponse Success<TResponse>(TResponse response, string messageCode = "1", string messageText = "操作成功") where TResponse : Response
        {
            response.Message = new Message()
            {
                MessageCode = messageCode,
                MessageText = messageText,
                IsSuccessful = true
            };
            return response;
        }

        private TResponse Fail<TResponse>(TResponse response, string messageCode = "0", string messageText = "操作失败") where TResponse : Response
        {
            response.Message = new Message()
            {
                MessageCode = messageCode,
                MessageText = messageText,
                IsSuccessful = false
            };
            return response;
        }

        /// <summary>
        /// 根据用户ID获取用户信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public GetUserByIdResponse GetUserById(GetUserByIdRequest request)
        {
            var user = userDtos.FirstOrDefault(item => item.UserId == request.UserId);

            var reponse = this.Success(new GetUserByIdResponse()
            {
                User = user
            });

            return reponse;
        }
    }
}
